<template>
  <el-dialog :visible.sync="visible" width="70%" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-col :span="24">
      <el-form v-loading="loading" :model="dataForm" :rules="dataRule" ref="dataForm" :label-width="$i18n.locale === 'en-US' ? '120px' : '120px'">
        <el-col :span="12">
          <!-- 队列编码 -->
          <el-form-item label="队列编码" prop="queueName">
            <!--文本框-->
            <el-input v-model="dataForm.queueName" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 消息业务key -->
          <el-form-item label="消息业务key" prop="msgBizKey">
            <!--文本框-->
            <el-input v-model="dataForm.msgBizKey" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 消息创建时间 -->
          <el-form-item label="消息创建时间" prop="msgCreateTime">
            <!--日期框-->
            <el-input v-model="dataForm.msgCreateTime" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 消费状态 0-消费失败 1-消费成功 -->
          <el-form-item label="消费状态" prop="feedStatus">
            <!--下拉框-->
            <el-select v-model="dataForm.feedStatus" disabled>
              <el-option
                :key="true"
                label="消费成功"
                :value="true"></el-option>
              <el-option
                :key="false"
                label="消费失败"
                :value="false"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 消费开始时间 -->
          <el-form-item label="消费开始时间" prop="feedStartTime">
            <!--日期框-->
            <el-input v-model="dataForm.feedStartTime" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 消费结束时间 -->
          <el-form-item label="消费结束时间" prop="feedEndTime">
            <!--日期框-->
            <el-input v-model="dataForm.feedEndTime" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 耗时（毫秒） -->
          <el-form-item label="耗时（毫秒）" prop="feedDuration">
            <!--文本框-->
            <el-input v-model="dataForm.feedDuration" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 消息内容json串 -->
          <el-form-item label="消息内容json串" prop="msgContent">
            <!--文本框-->
            <el-input v-model="dataForm.msgContent" type="textarea" :rows="3" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 消费状态描述 -->
          <el-form-item label="消费状态描述" prop="feedDesc">
            <!--文本框-->
            <el-input v-model="dataForm.feedDesc" type="textarea" :rows="3" disabled></el-input>
          </el-form-item>
        </el-col>
      </el-form>
    </el-col>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
    </template>
  </el-dialog>
</template>
<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      loading: false,
      visible: false,
      dataForm: {
        id: '',
        queueName: '',
        queueDesc: '',
        msgBizKey: '',
        msgContent: '',
        msgCreateTime: '',
        feedStatus: '',
        feedDesc: '',
        feedStartTime: '',
        feedEndTime: '',
        feedDuration: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {}
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.loading = true
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/queue/apiQueueLog/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return false
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.loading = true
        this.$http[!this.dataForm.id ? 'post' : 'put']('/queue/apiQueueLog/', this.dataForm).then(({ data: res }) => {
          this.loading = false
          if (res.code !== 0) {
            return false
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
